const app = getApp();

Page({
  data: {
    workOrders: []
  },
  onLoad: function () {
    const token = app.globalData.token;
    if (!token) {
      wx.showToast({
        title: '未获取到有效token，请先登录',
        icon: 'none'
      });
      return;
    }
    let url = 'http://8.140.225.174:5000/fultlist_progress';
    let header = {
      'content-type': 'application/x-www-form-urlencoded',
      'token': app.globalData.token
    };
    this.sendGetWorkOrdersRequest(url, header).then((resData) => {
      if (resData.code === 200 && resData.data) {
        let processedWorkOrders = resData.data.map(order => {
          return {
            createTime: order.CreateTime,
            faultArea: order.FaultArea,
            faultType: order.Faulttype,
            repairStatus: order.ListState
          };
        });
        this.setData({
          workOrders: processedWorkOrders
        });
      } else {
        wx.showToast({
          title: '获取报修单失败',
          icon: 'none'
        });
      }
    }).catch((error) => {
      wx.showToast({
        title: '网络错误',
        icon: 'none'
      });
    });
  },
  sendGetWorkOrdersRequest: function (url, header) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: url,
        method: 'GET',
        header: header,
        success: function (res) {
          console.log('网络请求成功，返回数据：', res);
          resolve(res.data);
        },
        fail: function (err) {
          reject(err);
        }
      });
    });
  }
})